package com.yc.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yc.R_Code;
import com.yc.Result;
import com.yc.entities.*;
import com.yc.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户角色关联表 前端控制器
 * </p>
 *
 * @author kuakha
 * @since 2021-04-07
 */
@RestController
@Slf4j
@Api(value = "BloodSugarUserRoleController", tags = {"添加院长"})
@RequestMapping("/bloodSugarUserRole")
public class BloodSugarUserRoleController {

    @Autowired
    private BloodSugarUserService bloodSugarUserService;
    @Autowired
    private BloodSugarUserRoleService bloodSugarUserRoleService;
    @Autowired
    private BloodSugarRoleService bloodSugarRoleService;
    @Autowired
    private BloodSugarPermissionService bloodSugarPermissionService;
    @Autowired
    private BloodSugarUserPermissionDepartmentService bloodSugarUserPermissionDepartmentService;
    @Autowired
    private BloodSugarDepartmentService bloodSugarDepartmentService;
    @Autowired
    private PasswordEncoder passwordEncoder;

    @ApiOperation(value = "pc添加院长账号")
//    @PreAuthorize("hasAnyAuthority('sys:bedManage','sys:noAccess')")
    @PostMapping("addDeanAccount")
    @Transactional(rollbackFor = Exception.class)
    public Result addDeanAccount(@Valid @RequestBody BloodSugarUser bloodSugarUser) {
        String encode = passwordEncoder.encode(bloodSugarUser.getPassword());
        bloodSugarUser.setPassword(encode);
        int status = bloodSugarUserService.saveDeanAccount(bloodSugarUser);
        if (status > 0) {
            LambdaQueryWrapper<BloodSugarRole> lambdaQuery = Wrappers.lambdaQuery();
            lambdaQuery.eq(BloodSugarRole::getRoleType,"1");
            BloodSugarRole one = bloodSugarRoleService.getOne(lambdaQuery);
            if (one==null){
                return Result.error().message("该角色不存在");
            }
            BloodSugarUserRole bloodSugarUserRole = new BloodSugarUserRole();
            bloodSugarUserRole.setRoleId(one.getId())
                    .setUserId(bloodSugarUser.getId());
            bloodSugarUserRoleService.save(bloodSugarUserRole);
            //查询科室信息
//            List<BloodSugarDepartment> list1 = bloodSugarDepartmentService.list();
            //查询权限信息  后期维护的话需要加权限模板表
            LambdaQueryWrapper<BloodSugarPermission> wrapper = Wrappers.lambdaQuery();
            wrapper.eq(BloodSugarPermission::getDeanId,2);
            List<BloodSugarPermission> list = bloodSugarPermissionService.list(wrapper);
            List<BloodSugarUserPermissionDepartment> bloodSugarUserPermissionDepartments = new ArrayList<>();
            list.forEach(f1->{
                BloodSugarUserPermissionDepartment bloodSugarUserPermissionDepartment = new BloodSugarUserPermissionDepartment();
                bloodSugarUserPermissionDepartment.setUserId(bloodSugarUser.getId())
                        .setDepartmentId(null)
                        .setPermissionId(f1.getId());
                bloodSugarUserPermissionDepartments.add(bloodSugarUserPermissionDepartment);
            });
            boolean b = bloodSugarUserPermissionDepartmentService.saveBatch(bloodSugarUserPermissionDepartments);
            if (b) {
                return Result.ok();
            }else {
                return Result.error().code(R_Code.R_ADD_FAIL);
            }
        }
        return Result.error().code(R_Code.R_ADD_FAIL);
    }




}

